# Set the following line to point to the replication directory
#PROJECT_DIR="/path_to_replication_directory/"

# Set up directory structure
cd "$PROJECT_DIR"
mkdir working output 
mkdir output/plots output/tables output/useful_numbers
mkdir output/plots/joint_loadings output/plots/conjoint_amce output/plots/conjoint_amce_validation output/plots/descriptives output/plots/factor_analysis_descriptive output/plots/individual_loadings output/plots/joint_loadings
mkdir output/tables/conjoint output/tables/joint_loadings
mkdir paper/inputs
mkdir logs

# Run code
cd "$PROJECT_DIR/code"
Rscript 00_source.R 

# Join logs
cd "$PROJECT_DIR/logs"
cat *.log >log.txt

# Move files and rename for inclusion in manuscript
cd "$PROJECT_DIR"
cp output/plots/descriptives/preference_densities_attn_new.pdf paper/inputs/figure_two.pdf
cp output/plots/factor_analysis_descriptive/uk_bivariate_attn.pdf paper/inputs/figure_three.pdf
cp output/plots/factor_analysis_descriptive/us_bivariate_attn.pdf paper/inputs/figure_four.pdf
cp output/plots/factor_analysis_descriptive/de_bivariate_attn.pdf paper/inputs/figure_five.pdf
cp output/plots/conjoint_amce/conjoint_amce_combined_attn.pdf paper/inputs/figure_six.pdf

# Compile manuscript
cd "$PROJECT_DIR/paper"
filename="manuscript"
pdflatex "$filename.tex"
bibtex "$filename"
pdflatex "$filename.tex"
pdflatex "$filename.tex"
# Remove intermediate files
rm "$filename.aux" "$filename.bbl" "$filename.blg" "$filename.log" "$filename.out" "$filename.dvi" "$filename.ptc" "$filename.synctex.gz" "app.aux"

# Compile appendix
filename="appendix"
pdflatex "$filename.tex"
bibtex "$filename"
pdflatex "$filename.tex"
pdflatex "$filename.tex"
# Remove intermediate files
rm "$filename.aux" "$filename.bbl" "$filename.blg" "$filename.log" "$filename.out" "$filename.dvi" "$filename.ptc" "$filename.synctex.gz" "app.aux"
rm ../code/Rplots.pdf
